package org.stvd.repository.admin.impl;

import java.util.Date;

import org.springframework.stereotype.Repository;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.admin.Loginfo;
import org.stvd.repository.admin.LoginfoDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("LoginfoDao")
public class LoginfoDaoImpl extends BaseDaoImpl<Loginfo> implements LoginfoDao {
    public QueryResult<Loginfo> getLoginfoResult(int start, int limit, String user_id, String role_id, String log_type,
            String log_type2, Date beginDate, Date endDate, String dep_id) {

        String hql = "select L1 from Loginfo L1 " + "where L1.oper_datetime between ?2 and ?3 "
                + "and ( L1.user_id = ?0 or '' = ?0 ) " + "and (L1.log_type like ?1 or L1.log_type2 like ?5 ) "
                + "and (L1.dep_id like ?4 or '' = ?4 ) " + "and (L1.role_id = ?6 or '' = ?6 ) "
                + "order by L1.oper_datetime desc ";

        return getQueryResultByHQL(start, limit, hql, user_id, "%" + log_type + "%", beginDate, endDate, dep_id,
                "%" + log_type2 + "%", role_id);
    }

    public boolean Emptyloginfo(String user_id, String log_type, Date beginDate, Date endDate, String dep_id) {
        String hql = "insert into loginfo_backup " + "select * from Loginfo L1 " + "where L1.oper_datetime "
                + "between :beginDate and :endDate " + "and ( L1.user_id = :user_id or '' = :user_id ) "
                + "and ( L1.log_type = :log_type or '' = :log_type ) "
                + "and (L1.dep_id like :dep_id or '' = :dep_id )";

        em.clear();
        em.createNativeQuery(hql).setParameter("user_id", user_id).setParameter("log_type", log_type)
                .setParameter("beginDate", beginDate).setParameter("endDate", endDate).setParameter("dep_id", dep_id)
                .executeUpdate();
        em.clear();
        em.createNativeQuery("delete from loginfo").executeUpdate();
        return true;
    }

    @Override
    public QueryResult<Loginfo> getLoginfoResult(int start, int limit, String username, String log_type,
            String log_type2, Date beginDate, Date endDate) {
        String hql = "select L1 from Loginfo L1 " + "where L1.oper_datetime between ?1 and ?2 "
                + "and ( L1.user_name = ?0 or '' = ?0 ) " + "and (L1.log_type like ?3 or L1.log_type2 like ?4 ) "
                + "order by L1.oper_datetime desc ";

        return getQueryResultByHQL(start, limit, hql, username, beginDate, endDate, "%" + log_type + "%",
                "%" + log_type2 + "%");
    }
}
